//
//  WXPDeferRepaymentVC.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/18.
//
//

#import "WXPDeferRepaymentVC.h"
#import "WXPDeferRepaymentHeaderView.h"
#import "WXPDeferRepaymentWayView.h"

@interface WXPDeferRepaymentVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;

@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) WXPDeferRepaymentHeaderView * detailHeaderView;
@property (nonatomic, strong) WXPDeferRepaymentWayView * paymentWayView;
@property (nonatomic, strong) ModelRepaymentDetail * repaymentDetailModel;

@end

@implementation WXPDeferRepaymentVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        [_navView resetViewWithTitle:@"Ir a pagar prórroga de periodo"];
        _navView.leftTop = XY(0, 0);
    }
    return  _navView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}
- (UIView *)headerView{
    if (_headerView == nil) {
        _headerView = [UIView  new];
    }
    return  _headerView;
}
- (WXPDeferRepaymentHeaderView *)detailHeaderView
{
    if (_detailHeaderView == nil) {
        _detailHeaderView = [WXPDeferRepaymentHeaderView  new];
    }
    return  _detailHeaderView;
}
- (WXPDeferRepaymentWayView *)paymentWayView
{
    if (_paymentWayView == nil) {
        _paymentWayView = [WXPDeferRepaymentWayView  new];
        WEAKSELF
        _paymentWayView.blockResetHeaderView = ^(ModelRepaymentCodeList * model) {
            [GlobalMethod hitPointWithPageName:KEY_DelayRepaymentMethod action:KEY_Enter extInfo:@{@"type": model.repaymentBank, @"loanOrderId": weakSelf.repaymentDetailModel.orderNo}];
            [GlobalMethod hitPointWithPageName:KEY_Repayment action:KEY_Enter extInfo:@{@"type": model.tag==1?@"BANCOS":model.tag==2?@"OpenPay":@"OXXO", @"loanOrderId": weakSelf.repaymentDetailModel.orderNo}];
            [weakSelf resetHeaderView];
        };
    }
    return  _paymentWayView;
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self requestDetail];
}
- (void)configUI{
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.agreementView];
    self.tableView.height = self.agreementView.top-self.navView.bottom-10;
    self.tableView.top = self.navView.bottom+10;
    [self resetHeaderView];
}

#pragma mark - resetHeaderView
- (void)resetHeaderView{
    self.headerView = [UIView initWithViews:@[
        self.detailHeaderView,
        self.paymentWayView,
    ]];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - request
- (void)requestDetail{
    [RequestApi requestRepaymentDetailWithOrderNo:self.modelPostpone.orderNo requestType:@"2" delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        self.repaymentDetailModel = [GlobalMethod exchangeDicToModel:response modelName:@"ModelRepaymentDetail"];
        [self.detailHeaderView resetViewWithModelPostpone:self.modelPostpone repaymentDetailModel:self.repaymentDetailModel orderDetailModel:self.orderDetailModel];

        // 付款方式 还款共有2种方式，bancos和Paynet。每种方式取一个，取不到的则不展示该还款方式。
        // 1：先添加 bancos 方式
        NSMutableArray * ary = [NSMutableArray new];
        for (ModelRepaymentCodeList * model in self.repaymentDetailModel.repaymentCodeList) {
            if(![model.repaymentBank isEqualToString:@"OXXO"]){
                // 不是OXXO，就是bancos方式
                model.imageName = @"icon-bancos-unselect";
                model.highImageName = @"icon-bancos-select";
                model.tag = 1;
                [ary addObject:model];
                break;
            }
        }
        // 2：添加 Paynet 方式
        if(isStr(self.repaymentDetailModel.openPayCode.paymentCode)){
            // 先判断openPayCode是否为空，不为空则取此字段数据
            self.repaymentDetailModel.openPayCode.imageName = @"icon-paynet-unselect";
            self.repaymentDetailModel.openPayCode.highImageName = @"icon-paynet-select";
            self.repaymentDetailModel.openPayCode.tag = 2;
            [ary addObject:self.repaymentDetailModel.openPayCode];
        }else{
            // 若openPayCode为空，则取repaymentCodeList字段列表中repaymentBank为"OXXO"的数据
            for (ModelRepaymentCodeList * model in self.repaymentDetailModel.repaymentCodeList) {
                if([model.repaymentBank isEqualToString:@"OXXO"]){
                    // 不是OXXO，就是bancos方式
                    model.imageName = @"icon-oxxo-unselect";
                    model.highImageName = @"icon-oxxo-select";
                    model.tag = 3;
                    [ary addObject:model];
                    break;
                }
            }
        }
        ModelRepaymentCodeList * model = ary.firstObject;
        model.isSelected = true;
        [self.paymentWayView resetPaymenyWayViewWithAry:ary];

        [self resetHeaderView];
        
        [GlobalMethod hitPointWithPageName:KEY_DelayRepaymentMethod action:KEY_Enter extInfo:@{@"type": model.repaymentBank, @"loanOrderId": self.repaymentDetailModel.orderNo}];
        [GlobalMethod hitPointWithPageName:KEY_Repayment action:KEY_Enter extInfo:@{@"type": model.tag==1?@"BANCOS":model.tag==2?@"OpenPay":@"OXXO", @"loanOrderId": self.repaymentDetailModel.orderNo}];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
